//
//  ShouCangViewController.m
//  Tour
//
//  Created by 才诗琦 on 16/3/16.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "ShouCangViewController.h"
#import "QpFoodDetailsViewController+shouCang.h"
#import "ShipinshouchangCell.h"
#import "MYShipinshouchangModel.h"
#import "DserXiangqingVC.h"
#import "DsCellVC.h"
#import "AppDelegate.h"
@interface ShouCangViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)BaseTableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataArr;/**<  没事收藏数组 */

@property (nonatomic, strong) NSMutableArray *shipinArr;/**<  视频数组 */

@property(nonatomic, strong)NSString *str;
@property (nonatomic, strong) NSMutableArray *youjiArr;

@property(nonatomic, strong)NSIndexPath *index;
@end

@implementation ShouCangViewController

-(void)viewWillAppear:(BOOL)animated
{
    [[DataKu shareInstance] openDb];
    [[DataKu shareInstance] creteTable];
    /**<  视频数组 */
    self.shipinArr = [NSMutableArray array];
    self.shipinArr = [[DataKu shareInstance] selet];
    
    [[Youjiku shareInstance] openDb];
    [[Youjiku shareInstance] creteTable];
    self.youjiArr = [NSMutableArray array];
    self.youjiArr = [[Youjiku shareInstance] selet];
    [self.tableView reloadData];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    // Do any additional setup after loading the view.
        self.dataArr = [NSMutableArray array];

    
  
    [self dataHandler];
    
    
    self.tableView = [[BaseTableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
   
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"666"]] ;

    //右按钮 编辑
    UIButton *edit = [UIButton buttonWithType:UIButtonTypeCustom];
    [edit setTitle:@"编辑" forState:UIControlStateNormal];
    [edit setTitle:@"完成" forState:UIControlStateSelected];
    
    edit.frame = CGRectMake(SCREEN_WIDTH - 60, 0, 50, 50);
    
    [edit addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    [edit setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [edit setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:edit];
    
    
    

    ///注册重用池
    [_tableView registerClass:[EatTableViewCell class] forCellReuseIdentifier:@"eat"];
    /**<  视频 */
     [self.tableView registerClass:[ShipinshouchangCell class] forCellReuseIdentifier:@"cell"];
    /**<  游记 */
    [self.tableView registerClass:[ShipinshouchangCell class] forCellReuseIdentifier:@"youji"];

    
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)editAction:(UIButton *)btn
{
    //bool状态反转
    btn.selected = !btn.selected;
    //通过buttonde的状态控制tableView
    [_tableView setEditing:btn.selected animated:YES];
    
}

#pragma mark - 是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
#pragma mark - 设置cell的编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //默认样式为删除
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - 删除确认信息
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return @"delete";
}

#pragma mark - 通过编辑状态 判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            
            
            
            [[Youjiku shareInstance] deleteNumber:[_youjiArr[indexPath.row] mingzi]];
            
            [_youjiArr removeObjectAtIndex:indexPath.row];
            
            [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            
        }
    }
    
    if (indexPath.section == 1) {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
           
            
            
            [[DataKu shareInstance] deleteNumber:[_shipinArr[indexPath.row] mingzi]];
            
            [_shipinArr removeObjectAtIndex:indexPath.row];
            
            [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }
    }
    
 
    if (indexPath.section ==2) {
        
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        _tableViewArr = [[[QpFoodDetailsViewController alloc] deleteHandlerWithArray:_tableViewArr index:indexPath.row] mutableCopy];
        [_tableViewArr removeObjectAtIndex:indexPath.row];
//        [_tempArr removeObjectAtIndex:indexPath.row];
        [_tableView reloadData];
    }
    }
}


- (void)dataHandler
{
    
//    self.dataArr = [NSMutableArray array];
    _dataArr = [@[@"游记收藏",@"视频收藏",@"美食收藏"] mutableCopy];
  self.tableViewArr = [[[QpFoodDetailsViewController alloc] selectAllModel] mutableCopy];
    
    

}

#pragma mark - tableView的协议实现方法

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

//头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

#pragma mark - 自定义头部区域
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];

    
    for (int i = 0; i < _dataArr.count; i++) {
        
        if (section == i) {
            
        
            UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];

            name.textAlignment = NSTextAlignmentCenter;
            name.text = _dataArr[i];
            
            [header addSubview:name];}

    }
        
       return header;
    
  
}
#pragma mark - 每个分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _youjiArr.count;
    }
    
    if (section ==1) {
        return _shipinArr.count;
    }
    
    
    if (section == 2) {
        return _tableViewArr.count;
    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        ShipinshouchangCell *cell = [tableView dequeueReusableCellWithIdentifier:@"youji" forIndexPath:indexPath];
        MYShipinshouchangModel *m = _youjiArr[indexPath.row];
        [cell.tupian sd_setImageWithURL:[NSURL URLWithString:m.tupian]];
        cell.zi.text = m.mingzi;
        return cell; 
    }
    
    
    if (indexPath.section == 1) {
        ShipinshouchangCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        MYShipinshouchangModel * m = _shipinArr[indexPath.row];
        
        [cell.tupian sd_setImageWithURL:[NSURL URLWithString:m.tupian]];
        cell.zi.text = m.mingzi;
        
        return cell;
    }
    
    
    
    if (indexPath.section == 2) {
        

    EatTableViewCell *eatCell = [tableView dequeueReusableCellWithIdentifier:@"eat" forIndexPath:indexPath];

    [eatCell.photo sd_setImageWithURL:[NSURL URLWithString:[_tableViewArr[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    //中文名字
    eatCell.cnname.text = [_tableViewArr[indexPath.row] title];
     return eatCell;
    }
    return nil;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
    
}

#pragma mark - tableView的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.hidesBottomBarWhenPushed = YES;
    
    
    if (indexPath.section == 0) {
         MYShipinshouchangModel *m = _youjiArr[indexPath.row];
        DsCellVC *vc = [[DsCellVC alloc]init];
        vc.userid = m.idddd;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    if (indexPath.section == 1) {
//        DserXiangqingVC *xiangqing = [[DserXiangqingVC alloc]init];
//        [self.navigationController pushViewController:xiangqing animated:YES];
    }
    
    
    
    
    
    if (indexPath.section == 2) {
        
    QpFoodDetailsViewController *qp = [[QpFoodDetailsViewController alloc] init];
    
        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:qp];
        
    qp.cityid = [_tableViewArr[indexPath.row] cityid];
        _index = indexPath;

    [self.navigationController presentViewController:na animated:YES completion:^{
        qp.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init];

        qp.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backM)];
        
    }];}

 
    self.hidesBottomBarWhenPushed = NO;
    
}


- (void)backM
{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
